< previous page page_180 next page >

Page 180
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
"RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _
Long) As Long

res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, controlkey, KEY_READ, 0, rootkey)
Now look at the function parameters one at a time. We have the key, the subkey the . . . wait a minute . . . are the access rights specified with the ulOptions parameter or the samDesired parameter?
You have it! The KEY_READ and 0 parameters are swapped. Change the code to
res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, controlkey, 0, KEY_READ, rootkey)
It works!
By the Way . . .
You might think that both of these puzzles are artificialthat I came up with them just to confuse you and perhaps remind you that you can run into API coding bugs even when your declaration is correct.
I wish I could take the credit for such cleverness (or is it deviousness?).
The truth is, I was expecting my first registry-related puzzle to be Puzzle 12, since enumerating keys is a somewhat tricky process. But you see, I actually do follow my own advice and implement API-based code in small sections, testing each one along the way. The two problems you see here were in my original code. And although I will concede (with all due modesty) that it took me less than five minutes to solve both of them, it was clear to me that they represented real problems anyone can run into and were thus deserving of their own puzzle.

 
< previous page page_180 next page >